package huajiu.com.utlis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;


public class JSAPITicketTool {
	 public static String sendGet(String url, String charset, int timeout)
     {
       String result = "";
       try
       {
         URL u = new URL(url);
         try
         {
           URLConnection conn = u.openConnection();
           conn.connect();
           conn.setConnectTimeout(timeout);
           BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
           String line="";
           while ((line = in.readLine()) != null)
           {
            
             result = result + line;
           }
           in.close();
         } catch (IOException e) {
           return result;
         }
       }
       catch (MalformedURLException e)
       {
         return result;
       }
      
       return result;
     }
    /***
     * 获取acess_token 
     * 来源www.vxzsk.com
     * @return
     */
    public static String getAccessToken(){
           String appid=PayConfigUtil.APP_ID;//应用ID
           String appSecret=PayConfigUtil.APP_SECRET;//(应用密钥)
           String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+"";
           String backData=JSAPITicketTool.sendGet(url, "utf-8", 10000);
           String acess_token =(String)JSONObject.fromObject(backData).get("access_token");
           System.out.println(acess_token);
           return acess_token;
           
    }
   /***
     * 获取jsapiTicket
     * 来源 www.vxzsk.com
     * @return
     */
   public static String getJSApiTicket(){ 
       //获取token
       String acess_token= JSAPITicketTool.getAccessToken();
          
       String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi";  
       String backData=JSAPITicketTool.sendGet(urlStr, "utf-8", 10000);  
       String ticket = (String) JSONObject.fromObject(backData).get("ticket");  
       return  ticket;  
          
   }  
   
}
